package com.example.fw;

import org.openqa.selenium.By;
import com.example.tests.ContactObject;

public class ContactHelper extends HelperWithWebDriverBase {

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public void createContact(ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		manager.getNavigationHelper().goToAddNewContactPage();
		fillContactForm(contactObject, true);
		submitContactCreationForm();
		returnToHomePage();
	}

	public void deleteContact(int i) {
		manager.getNavigationHelper().openMainPage();
		initContactEdition(i);
		submitContactDeletion();
		returnToHomePage();

	}

	public void modifyContact(int i, ContactObject contactObject) {
		manager.getNavigationHelper().openMainPage();
		initContactEdition(i);
		fillContactForm(contactObject, false);
		submitContactModification();
		returnToHomePage();
	}

	private void initContactEdition(int i) {
		click(By.cssSelector("img[alt=\"Edit\"]"));

	}

	private void fillContactForm(ContactObject contactObject, boolean fillGroup) {
		type(By.name("firstname"), contactObject.name);
		type(By.name("lastname"), contactObject.surname);
		type(By.name("address"), contactObject.homeAdress);
		type(By.name("home"), contactObject.homePhone);
		type(By.name("mobile"), contactObject.mobilePhone);
		type(By.name("work"), contactObject.workPhone);
		type(By.name("email"), contactObject.emailPrime);
		type(By.name("email2"), contactObject.emailSecond);
		select(By.name("bday"), contactObject.birthDay);
		select(By.name("bmonth"), contactObject.birthMonth);
		type(By.name("byear"), contactObject.birthYear);
		if (fillGroup) {
			select(By.name("new_group"), contactObject.groupName);
		} else {
			// ......
		}
		type(By.name("address2"), contactObject.homeAddressSecond);
		type(By.name("phone2"), contactObject.homePhoneSecond);

	}

	private void submitContactCreationForm() {
		click(By.name("submit"));
	}

	private void submitContactModification() {
		click(By.name("update"));

	}

	private void submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}

	private void returnToHomePage() {
		click(By.linkText("home page"));
	}

}
